package com.crop.back.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crop.back.common.enums.Deleted;
import com.crop.back.common.utils.ResultBean;
import com.crop.back.dto.ProductCategoryDTO;
import com.crop.back.dto.QualityInspectionDTO;
import com.crop.back.entity.ProductCategory;
import com.crop.back.entity.QualityInspection;
import com.crop.back.service.ProcessingBatchService;
import com.crop.back.service.ProductCategoryService;
import com.crop.back.service.QualityInspectionService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.annotation.Resource;

/**
 * <p>
 * 质量检测记录表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2025-01-20
 */
@RestController
@RequestMapping("/quality-inspection")
public class QualityInspectionController {
    @Resource
    private QualityInspectionService inspectionService;
    @Resource
    private ProcessingBatchService processingBatchService;

    @PostMapping("/list")
    public ResultBean getAll(@RequestBody QualityInspectionDTO inspectionDTO) {
        Integer page = inspectionDTO.getPage();
        Integer pageSize = inspectionDTO.getPageSize();
        List<Long> batchNo = processingBatchService.getBatchNo();
        Page<QualityInspection> pageInfo = new Page<>(page, pageSize);
        Page<QualityInspection> res = inspectionService.page(pageInfo, new LambdaQueryWrapper<QualityInspection>()
                .eq(QualityInspection::getDeleted, Deleted.NOT_DELETED.getCode())
                .in(batchNo != null && !batchNo.isEmpty(), QualityInspection::getBatchId, batchNo));
        return ResultBean.ok(res.getRecords(), res.getTotal());
    }

    @PostMapping("/get")
    public ResultBean getByCondition(@RequestBody QualityInspectionDTO inspectionDTO) {
        Integer page = inspectionDTO.getPage();
        Integer pageSize = inspectionDTO.getPageSize();
        Page<QualityInspection> pageInfo = new Page<>(page, pageSize);
        List<Long> batchNo = processingBatchService.getBatchNo();
        LambdaQueryWrapper<QualityInspection> queryWrapper = new LambdaQueryWrapper<QualityInspection>()
                .eq(QualityInspection::getDeleted, Deleted.NOT_DELETED.getCode())
                .like(inspectionDTO.getInspector() != null, QualityInspection::getInspector, inspectionDTO.getInspector());
        queryWrapper.ge(inspectionDTO.getStartInspectionTime() != null, QualityInspection::getInspectionTime, inspectionDTO.getStartInspectionTime());
        queryWrapper.le(inspectionDTO.getEndInspectionTime() != null, QualityInspection::getInspectionTime, inspectionDTO.getEndInspectionTime());
        if (batchNo != null && !batchNo.isEmpty()) {
            queryWrapper.in(QualityInspection::getBatchId, batchNo);
        }
        Page<QualityInspection> res = inspectionService.page(pageInfo, queryWrapper);
        return ResultBean.ok(res.getRecords(), res.getTotal());
    }

    @PostMapping("/save")
    public ResultBean addOrUpdate(@RequestBody QualityInspectionDTO inspectionDTO) {
        try {
            QualityInspection qualityInspection = BeanUtil.copyProperties(inspectionDTO, QualityInspection.class);
            if (qualityInspection.getId() == null) {
                inspectionService.save(qualityInspection);
                return ResultBean.ok("添加成功");
            } else {
                inspectionService.updateById(qualityInspection);
                return ResultBean.ok("修改成功");
            }
        } catch (Exception e) {
            if (e.getMessage().contains("Duplicate entry") || e.getCause().getMessage().contains("Duplicate entry")) {
                return ResultBean.fail("类别名已存在，请检查重复项");
            }
            return ResultBean.fail("保存失败");
        }

    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id) {
        try {
            LambdaUpdateWrapper<QualityInspection> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(QualityInspection::getId, id);
            updateWrapper.set(QualityInspection::getDeleted, Deleted.DELETED.getCode());
            inspectionService.update(updateWrapper);
            return ResultBean.ok("删除成功");
        } catch (Exception e) {
            return ResultBean.fail("删除失败");
        }
    }

}
